﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using HashFoo.Rest.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.Rest.CommonTests
{
    [TestClass]
    public class ResourceUriCollectionTests
    {
        [TestMethod]
        [Description("Asserts that the collection can be built from an enumeration")]
        public void CanCreateFromEnumerable()
        {
            var resources = new[]
            {
                new ResourceUri { ResourceType = GetType(), Uri = "uri1" },
                new ResourceUri { ResourceType = typeof(string), Uri = "stringUri" }
            };

            var collection = new ResourceUriCollection(resources);

            Assert.AreEqual(2, collection.Count());
        }

        [TestMethod]
        [Description("Tests that if duplicates are passed in the underlying collection, they are removed")]
        public void CanCreateWithDuplicates()
        {
            var resources = new[]
            {
                new ResourceUri { ResourceType = GetType(), Uri = "uri1" },
                new ResourceUri { ResourceType = GetType(), Uri = "uri2" },
                new ResourceUri { ResourceType = typeof(string), Uri = "stringUri" }
            };

            var collection = new ResourceUriCollection(resources);

            Assert.AreEqual(2, collection.Count());
        }

        [TestMethod]
        [Description("Tests that enumeration works")]
        public void CanEnumerate()
        {
            var resources = new[]
            {
                new ResourceUri { ResourceType = GetType(), Uri = "uri1" },
                new ResourceUri { ResourceType = typeof(string), Uri = "stringUri" }
            };

            var collection = new ResourceUriCollection(resources);

            var count = 0;
            foreach (var resourceUri in collection)
            {
                Assert.IsNotNull(resourceUri);
                count++;
            }

            Assert.AreEqual(2, count);
        }

        [TestMethod]
        [Description("Tests resource type based uri lookup")]
        public void CanLookupByType()
        {
            const string uriToLookup = "stringUri";
            var typeToLookup = typeof(string);

            var resources = new[]
            {
                new ResourceUri { ResourceType = GetType(), Uri = "uri1" },
                new ResourceUri { ResourceType = typeToLookup, Uri = uriToLookup }
            };

            var collection = new ResourceUriCollection(resources);

            var uriResult = collection.GetUri(typeToLookup);
            Assert.AreEqual(uriToLookup, uriResult);

            var templatedUriResult = collection.GetUri<string>();
            Assert.AreEqual(uriToLookup, templatedUriResult);
        }
    }
}
